unit fHostForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, uHYModuleManager, uHYIntf;

type
  TForm1 = class(TForm)
    bLoad: TButton;
    bUnload: TButton;
    HostPanel: TPanel;
    ModuleManager: THYModuleManager;
    procedure bLoadClick(Sender: TObject);
    procedure bUnloadClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    fPluginForm : IHYVisualPlugin;

    procedure SwitchButtons;
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
  SwitchButtons;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // Makes sure the reference to the plugin is released so that the DLL
  // can unload successfully

  ModuleManager.ReleaseInstance(fPluginForm);
end;

procedure TForm1.bLoadClick(Sender: TObject);
begin
  with ModuleManager do begin
    // Loads the module
    LoadModule('VisualModule.dll');

    // Creates an instance of the visual plugin
    CreateVisualPlugin('MyFirstVisualPlugin', fPluginForm); // See the initialization part of the unit fVisualPlugin.pas

    // Displays it
    fPluginForm.ShowParented(HostPanel);
  end;

  SwitchButtons;
end;

procedure TForm1.bUnloadClick(Sender: TObject);
begin
  // Releases the reference to the plugin (equivalent to "fPluginForm := NIL")
  ModuleManager.ReleaseInstance(fPluginForm);

  // Unloads the module
  ModuleManager.UnloadModule(0);

  SwitchButtons;
end;

procedure TForm1.SwitchButtons;
begin
  bLoad.Enabled := ModuleManager.ModuleCount=0;
  bUnload.Enabled := not bLoad.Enabled;
end;

end.
